package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 添加套餐
     *
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody SetmealDto setmealDto) {
        setmealService.saveWithDish(setmealDto);


        return R.success("保存成功");
    }

    /**
     * 分页查询
     *
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {

        //分页构造器
        Page<Setmeal> pageinfo = new Page<>(page, pageSize);
        //查询条件
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Setmeal::getUpdateTime);
        //lqw.eq(Setmeal::getStatus, 1);
        //查询方法
        setmealService.page(pageinfo, lqw);


        Page<SetmealDto> objectPage = new Page<>();
        //拷贝
        BeanUtils.copyProperties(pageinfo, objectPage, "records");

        List<Setmeal> records = pageinfo.getRecords();

        ArrayList<SetmealDto> setmealDtos = new ArrayList<>();

        for (Setmeal record : records) {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(record, setmealDto);
            //设置分类名称
            Category byId = categoryService.getById(record.getCategoryId());
            setmealDto.setCategoryName(byId.getName());
            setmealDtos.add(setmealDto);
        }
        objectPage.setRecords(setmealDtos);
        return R.success(objectPage);

    }

    /**
     * 批量删除
     *
     * @param list
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "setmealCache", allEntries = true)////清除setmealCache名称下,所有的缓存数据
    public R<String> delete(@RequestParam("ids") List<Long> list) {
        log.info("批量删除接收到的id:{}", list);
        setmealService.delete(list);

        return R.success("欧克");
    }


    /**
     * 根据分类id查询套餐
     */
    @Cacheable(value = "setmealCache", key = "#setmeal.CategoryId+'_'+#setmeal.Status")
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal) {
        log.info("根据id查询套餐：{}", setmeal);
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId, setmeal.getCategoryId());
        wrapper.eq(setmeal.getStatus() != null, Setmeal::getStatus, setmeal.getStatus());
        wrapper.orderByDesc(Setmeal::getUpdateTime);

        List<Setmeal> setmeals = setmealService.list(wrapper);

        return R.success(setmeals);

    }

    /**
     * 批量启售/停售
     *
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> status(@PathVariable int status, @RequestParam("ids") List<Long> ids) {
        log.info("批量操作数据：{}", ids);
        List<Setmeal> setmeals = setmealService.listByIds(ids);
        for (Setmeal setmeal : setmeals) {
            setmeal.setStatus(status);
        }
        setmealService.updateBatchById(setmeals);
        return R.success("修改成功");
    }

    /**
     * 根据id查询菜品和对应的口味信息
     */
    @GetMapping("{id}")
    public R<SetmealDto> getByidWith(@PathVariable Long id) {
        SetmealDto setmealDto = setmealService.getByidWith(id);
        //  return R.success(SetmealDto);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("修改套餐的基本信息：{}", setmealDto);
        setmealService.updateAll(setmealDto);
        return R.success("套餐修改成功");
    }
}
